import { createSlice } from "@reduxjs/toolkit";
import { optionsTab } from '@/views/market/components/select-dropdown/tools.ts'

// 定义初始化状态
const initialState = { value: optionsTab[0].chv, list: [] };

// 创建切片
const counterSlice = createSlice({
  // 切片名称
  name: "router",
  // 初始化状态
  initialState,
  // 定义处理器
  reducers: {
    setValue: (state, action) => {
      state.value = [...action.payload]
    },
    setList: (state, action) => {
      state.value.forEach(element => {
        if (action.payload?.id === element.id) {
          if (!action.payload.isOk) {
            element.isOk = true
            state.list.push({ ...element })
          } else {
            element.isOk = false
            state.list = state.list.filter(res => res?.id !== action.payload?.id)
          }
        }
      });
    },
    setNewList: (state, action) => {
    }
  }
});

export const { setValue, setList, setNewList } = counterSlice.actions;

export default counterSlice.reducer;